
import util from "/common/js/util.js";
import Qs from "qs";

var app = getApp();


Page({
  data: {
    dataList:{},
    queryData:{},
    userInfo:{},
    basicVisible:false,
    t:new Date().getTime(),
    dis_txt:"" //不可点击按钮提示文字
  },
  onLoad(e) {
    console.log(e);
    this.setData({
      queryData:e
    });
    this.initFn()
  },
  initFn(){
    this.detailFn();
  },
  detailFn(){

    let list={
      goodId:this.data.queryData.goodId
    };
    let api="/user/good/goodDetail"
    app.$post(api, list).then(res => {
      if(res.code==200){
        let item=res.data;
        item.mainImgs=[];
        if(item.mainImg){
          item.mainImgs=item.mainImg.split(",");
          let time=new Date().getTime();
          item.mainImgs.forEach(element => {
            element+=`?time=${time}`
          });
          
        }
        item.details=[]
        if(item.detail){
          item.details=item.detail.split(",")

        }
        this.setData({
          dataList:item
        });
        this.getUserInfo();

      }else{
        util.message(res.message);

      }
    })
  },
  getUserInfo(){
    let api = "/user/info/getUserInfo", list = {};
    app.$post(api, list).then(re => {
      try {
         if (re.code == 200) {
          let dis_txt="";
          this.setData({
            userInfo:re.data
          })
          if(this.data.userInfo.integral<this.data.dataList.integral){
            dis_txt="积分不足"
          }
          if(this.data.dataList.stock<1){
            dis_txt="库存不足"
          }
          this.setData({
            dis_txt
          })
          
        }else {
          util.message(re.message)
        };
      } catch (error) {
        
      }

    }).catch(err => {
    });
  },
  previewImageFn(e) {
    let img = e.target.dataset.img;
    if (!img) {
      return;
    }
    dd.previewImage({
      urls: [
        img
      ],
      current: 1,
      success: () => {},
      fail: () => {},
      complete: () => {},
    });
  },
  previewImagesFn(e) {
    let mainImgs = this.data.dataList.mainImgs;
    let index = e.currentTarget.dataset.index;

    if (!mainImgs) {
      return;
    }
    dd.previewImage({
      urls: mainImgs,
      current: index,
      success: () => {},
      fail: () => {},
      complete: () => {},
    });
  },
  createFn(){
    dd.showLoading({
      content: '数据提交中...',
    })
    let api = "/user/good/buyNow", list = {
      goodId:this.data.dataList.goodId
    };

    app.$post(api, list).then(re => {
      try {
        dd.hideLoading();

         if (re.code == 200) {
          this.setData({
            loadding:false
          })
          this.handleOpen();
        }else {
          util.message(re.message)
        };
      } catch (error) {
        
      }

    }).catch(err => {
      
      
    });
  },
  submitFn(){
    this.setData({
      loadding:true
    });
    dd.confirm({
      title: '温馨提示',
      content: '是否进行兑换?',
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      success: (result) => {
        if (result.confirm) {
          this.createFn()
        }
      },
    });
   
  },
  handleOpen(){
    this.setData({
      basicVisible:true
    })
  },
  handleClose(){
    this.setData({
      basicVisible:false
    })
  },
  goToOrderListFn(){
    // let item = e.currentTarget.dataset.item;
    // let json={goodId:item.goodId}
    // let p = Qs.stringify(json);
    let path = '/pages/shop/order_list/order_list';
    let url = `${path}`;
    dd.navigateTo({
      url
    })
  }
});
